package com.shopping.www.util;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;


public class TimerUtil {


    private static TimerUtil timerUtil = new TimerUtil();

    public static TimerUtil getTimerUtil() {
        return timerUtil;
    }

    private Timer timer;
    private ExecutorService executor;
    private final Map<TimerTask, TimerTaskWrapper> wrappedTasks = new HashMap<TimerTask, TimerTaskWrapper>();


    private TimerUtil() {
        timer = new Timer("timer-syndata", true);
        executor = Executors.newCachedThreadPool(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable runnable) {
                Thread thread = new Thread(runnable);
                thread.setDaemon(true);
                if (thread.getPriority() != Thread.NORM_PRIORITY) {
                    thread.setPriority(Thread.NORM_PRIORITY);
                }
                return thread;
            }
        });
    }


    public Future<?> submit(Runnable task) {
        return executor.submit(task);
    }

    public void schedule(TimerTask task, long delay) {
        TimerTaskWrapper taskWrapper = new TimerTaskWrapper(task);
        synchronized (wrappedTasks) {
            wrappedTasks.put(task, taskWrapper);
        }
        timer.schedule(taskWrapper, delay);
    }

    public void schedule(TimerTask task, long delay, long period) {
        TimerTaskWrapper taskWrapper = new TimerTaskWrapper(task);
        synchronized (wrappedTasks) {
            wrappedTasks.put(task, taskWrapper);
        }
        timer.schedule(taskWrapper, delay, period);
    }

    public void scheduleAtFixedRate(TimerTask task, long delay, long period) {
        TimerTaskWrapper taskWrapper = new TimerTaskWrapper(task);
        synchronized (wrappedTasks) {
            wrappedTasks.put(task, taskWrapper);
        }
        timer.scheduleAtFixedRate(taskWrapper, delay, period);
    }


    public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) {
        TimerTaskWrapper taskWrapper = new TimerTaskWrapper(task);
        synchronized (wrappedTasks) {
            wrappedTasks.put(task, taskWrapper);
        }
        timer.scheduleAtFixedRate(taskWrapper, firstTime, period);
    }

    public void cancelScheduledTask(TimerTask task) {
        TimerTaskWrapper taskWrapper;
        synchronized (wrappedTasks) {
            taskWrapper = wrappedTasks.remove(task);
        }
        if (taskWrapper != null) {
            taskWrapper.cancel();
            task.cancel();
        }
    }


    public void dispose() {
        executor.shutdownNow();
        executor = null;


        timer.cancel();
        timer = null;


        timerUtil = null;
        wrappedTasks.clear();
    }


    private class TimerTaskWrapper extends TimerTask {

        private TimerTask task;


        public TimerTaskWrapper(TimerTask task) {
            this.task = task;
        }


        @Override
        public void run() {
            executor.submit(task);
        }

    }


}